package com.woyou.ui.activity;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Window;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.woyou.MyApplication;
import com.woyou.bean.Info;
import com.woyou.service.NetWorkCenter;
import com.woyou.service.ThreadPoolManager;
import com.woyou.ui.api.PwdErrorListener;
import com.woyou.ui.api.RetryNetwork;

@SuppressLint("NewApi")
@EActivity
public abstract class SuperFragmentActivity extends FragmentActivity implements RetryNetwork, PwdErrorListener{

	protected MyApplication app;
	
	protected boolean isAvtive = false;
	
	protected Context mContext;
	
	@Bean NetWorkCenter mNetWorkCenter;
	
	@Bean ThreadPoolManager mThreadPoolManager;
	
	public static final String INFO_NAME = "info";
	
	@SuppressWarnings("rawtypes")
	protected Info mInfo;

	
	/**
	 * 用于判断APP是否是正常启动,还是奔溃后自动重启的
	 */
	public static final String IS_NORMAL_START = "isNormalStart";

	/**
	 * 碎片管理器
	 */
	protected FragmentManager fmm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mContext = this;
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		isAvtive = true;
		
		fmm = getSupportFragmentManager();
	}
	
	@Override
	public void pwdError() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				//TODO 弹出密码错误dialog
				
			}
		});
	
	}
	
	/**
	 * 是否有数据传递过来
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public boolean hasInfo() {
		Intent intent = getIntent();
		if(intent!=null){
			mInfo = (Info) getIntent().getSerializableExtra(INFO_NAME);
			if (mInfo != null) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		// 如果是singleTask模式，每次需要重新获取Intent
		setIntent(intent);
		getIntent().putExtras(intent);
	}
	
	public ProgressDialog getProgressDialog(String msg) {
		ProgressDialog progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(msg);
		progressDialog.setCancelable(true);
		return progressDialog;
	}
	
	protected void showToast(final String msg){
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				Toast.makeText(SuperFragmentActivity.this, msg, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		isAvtive = false;
		mNetWorkCenter.removeRetry();
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		isAvtive = true;
		
		if( JPushInterface.isPushStopped(getApplicationContext()) ){
			JPushInterface.resumePush(getApplicationContext());
		}
		
		boolean isNormalStart = true;
		if( !isNormalStart ){
			//非正常启动
			// Intent i = new Intent(this, FlashActivity.class);
       	 	// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        	// startActivity(i);
		}
		mNetWorkCenter.setRetryNetwork(this);
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		isAvtive = false;
		mNetWorkCenter.removeRetry();
	}
	
	public boolean getAvtive(){
		return isAvtive;
	}
	
	
	protected void hintNoNetWork(){
		//TODO 弹出网络错误提示框
	}
	
	protected float getDimen(int id){
		float dimension = getResources().getDimension(id);
		return dimension;
	}
	
	protected void executeTask(Runnable run){
        mThreadPoolManager.executeTask(run);
	}
	
}
